package com.adee.gulimall.coupon.service.impl;

import com.adee.gulimall.common.to.MemberPrice;
import com.adee.gulimall.common.to.SkuReductionTo;
import com.adee.gulimall.common.utils.PageUtils;
import com.adee.gulimall.common.utils.Query;
import com.adee.gulimall.coupon.dao.SkuFullReductionDao;
import com.adee.gulimall.coupon.entity.MemberPriceEntity;
import com.adee.gulimall.coupon.entity.SkuFullReductionEntity;
import com.adee.gulimall.coupon.entity.SkuLadderEntity;
import com.adee.gulimall.coupon.service.MemberPriceService;
import com.adee.gulimall.coupon.service.SkuFullReductionService;
import com.adee.gulimall.coupon.service.SkuLadderService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    SkuLadderService skuLadderService;

    @Autowired
    SkuFullReductionService skuFullReductionService;

    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo to) {
        // sku的优惠信息和满减信息，跨库操作gulimall_sms，调用其他微服务
        // sms_sku_ladder打折表，sms_sku_full_reduction满减表，sms_member_price会员价格表，sms_spu_bounds:spu积分表
        // 1.保存ladder阶梯折扣价格，sms_sku_ladder打折表
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(to.getSkuId());
        skuLadderEntity.setFullCount(to.getFullCount());
        skuLadderEntity.setDiscount(to.getDiscount());
        skuLadderEntity.setAddOther(to.getCountStatus());
        if(to.getFullCount() > 0) {
            skuLadderService.save(skuLadderEntity);
        }
        // 2.保存满减信息，sms_sku_full_reduction满减表
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(to, skuFullReductionEntity);
        if(skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            this.save(skuFullReductionEntity);
        }

        // 3.保存会员价格，sms_member_price会员价格表
        List<MemberPrice> memberPriceList = to.getMemberPrice();
        List<MemberPriceEntity> list = memberPriceList.stream().map(m -> {
            MemberPriceEntity e = new MemberPriceEntity();
            e.setSkuId(to.getSkuId());
            e.setMemberLevelId(m.getId());
            e.setMemberLevelName(m.getName());
            e.setMemberPrice(m.getPrice());
            e.setAddOther(1);
            return e;
        }).filter(item -> item.getMemberPrice().compareTo(new BigDecimal("0")) == 1)
                .collect(Collectors.toList());
        memberPriceService.saveBatch(list);
    }

}